<?php

include('cd.php');
ini_set('max_execution_time', '3600');
ob_start();
$CurrentUser = Authentication::Authenticate();

if(!$CurrentUser->hasPermission(RIGHT_EXPORT_CSV))
{
	$e = new Error(RIGHTS_ERR_USERNOTALLOWED);
	Error::AddError($e);
	HTMLstuff::RefererRedirect();
}

$ModelID = Utils::SafeIntFromQS('model_id');
$includepath = Utils::SafeIntFromQS('includepath');
$includepath = in_array($includepath, array(EXPORT_PATH_OPTION_NONE, EXPORT_PATH_OPTION_RELATIVE, EXPORT_PATH_OPTION_FULL)) ? $includepath : EXPORT_PATH_OPTION_NONE;

$Models = Model::GetModels(new ModelSearchParameters(
	is_NULL($ModelID) ? FALSE : $ModelID));
$Sets = Set::GetSets(new SetSearchParameters(
	FALSE,
	FALSE,
	is_null($ModelID) ? FALSE : $ModelID));


$outfile = 'CandyDollDB.csv';
if($ModelID && count($Models) > 0){
	$Model = $Models[0];
	$outfile = sprintf('CandyDollDB %1$s.csv',$Model->GetFullName());
}

header(sprintf('Content-type: %1$s', Utils::GetMime('csv')));
header(sprintf('Content-Disposition: attachment; filename="%1$s"', $outfile));

$commentarray = array(
	sprintf("Generated by CandyDollDB v%1\$s on %2\$s at %3\$s", CANDYDOLLDB_VERSION, date('Y-m-d'), date('H:i;s')),
	sprintf("Author: %1\$s", $CurrentUser->getUserName()),
	"Project website: https://code.google.com/p/candydolldb/");
$commentcount = count($commentarray);
$x = 0;

/* @var $Model Model */
foreach ($Models as $Model)
{
	if($ModelID && $Model->getID() !== $ModelID)
	{ continue; }

	$SetsThisModel = Set::Filter($Sets, $Model->getID());
	if($SetsThisModel)
	{
		/* @var $Set Set */
		foreach ($SetsThisModel as $Set)
		{
			$ImagesThisSet = Image::GetImages(new ImageSearchParameters(FALSE, FALSE, $Set->getID(), FALSE, $Model->getID()));

			if($ImagesThisSet)
			{
				/* @var $Image Image */
				foreach($ImagesThisSet as $Image)
				{
					if(Utils::_empty($Image->getFileCRC32()))
					{ continue; }
					
					printf("%1\$s,%2\$s,%3\$s,\\%4\$s\\,%5\$s%6\$s",
						$Image->getExportFilename(),
						$Image->getFileSize(),
						$Image->getFileCRC32(),
						str_replace(CANDYPATH.DIRECTORY_SEPARATOR, '', dirname($Image->getFilenameOnDisk())),
						$x <= ($commentcount - 1) ? $commentarray[$x]."," : NULL,
						PHP_EOL);
					$x++;
				}
			}
			
			$VideosThisSet = Video::GetVideos(new VideoSearchParameters(FALSE, FALSE, $Set->getID(), FALSE, $Model->getID()));
			
			if($VideosThisSet)
			{
				/* @var $Video Video */
				foreach($VideosThisSet as $Video)
				{
					if(Utils::_empty($Video->getFileCRC32()))
					{ continue; }
					
					printf("%1\$s,%2\$s,%3\$s,\\%4\$s\\,%5\$s%6\$s",
						$Video->getExportFilename($includepath),
						$Video->getFileSize(),
						$Video->getFileCRC32(),
						str_replace(CANDYPATH.DIRECTORY_SEPARATOR, '', dirname($Video->getFilenameOnDisk())),
						$x <= ($commentcount - 1) ? $commentarray[$x]."," : NULL,
						PHP_EOL);
					$x++;
				}
				ob_flush();
				flush();
			}
			ob_flush();
			flush();
		}
		ob_flush();
		flush();
	}
	ob_flush();
	flush();
}
ob_end_flush();
flush();

exit;

?>